/*******************************************************************************
 * Copyright (c) 2007 TOPCASED. All rights reserved. This program
 * and the accompanying materials are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: - Topcased contributors and others - initial API and implementation
 *******************************************************************************/
/**
 * This is a fork of the UML2Java Plug-in developed by Topcased and released under
 * the Eclipse EPL License. http://www.eclipse.org/legal/epl-v10.html
 * In accordance with the license, I have forked the UML2Java plugin and have adapted
 * it to suit the needs of a UML2QoS instrumentation code generator for PHP.
 * 
 * @author Alexandru Burdusel
 */

package org.topcased.uml2qos.properties;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * Class that is used to add a property page for configurating the code generation
 * @author Ariel RAMPHUL (Sopra Group - Toulouse)
 */
public class Uml2QosProperties extends PropertyPage {
    
    private Button genAuthorButton;
    private Button genHeaderButton;
    private Button genAccessorsButton;    
    private Button genJavaDocButton;
    private Button useTopcasedChkstButton;
    private Button browseButton;
    private Text authorText;
    private Text headerText;
    private Text outputText;
    private Label authorLabel;
    private Label headerLabel;    
    private Label outputLabel;
    
    /**
     * Constructor for SamplePropertyPage.
     */
    public Uml2QosProperties() {
        super();
    }
    
    /**
     * Create and initalize widget on the property page
     * @param parent is the composite
     */
    private void addFirstSection(Composite parent) {
        Composite composite = createDefaultComposite(parent);

        genAuthorButton = new Button(composite, SWT.CHECK);
        genAuthorButton.setText("Generate author");
        FormData data1 = new FormData();
        data1.left = new FormAttachment(0,0);
        data1.right = new FormAttachment(100,-5);
        data1.top = new FormAttachment(64,10); 
        genAuthorButton.setLayoutData(data1);
        
        genHeaderButton = new Button(composite, SWT.CHECK);
        genHeaderButton.setText("Generate header");      
        FormData data2 = new FormData();
        data2.left = new FormAttachment(0,0);
        data2.right = new FormAttachment(100,-5);
        data2.top = new FormAttachment(genAuthorButton,5); 
        genHeaderButton.setLayoutData(data2);
        
        genAccessorsButton = new Button(composite, SWT.CHECK);
        genAccessorsButton.setText("Generate Accessors");      
        FormData data3 = new FormData();
        data3.left = new FormAttachment(0,0);
        data3.right = new FormAttachment(100,-5);
        data3.top = new FormAttachment(genHeaderButton,5); 
        genAccessorsButton.setLayoutData(data3);
        
        genJavaDocButton = new Button(composite, SWT.CHECK);
        genJavaDocButton.setText("Generate PHPdoc");      
        FormData data5 = new FormData();
        data5.left = new FormAttachment(0,0);
        data5.right = new FormAttachment(100,-5);
        data5.top = new FormAttachment(genAccessorsButton,5); 
        genJavaDocButton.setLayoutData(data5);    
        
        useTopcasedChkstButton = new Button(composite, SWT.CHECK);
        useTopcasedChkstButton.setText("Generate code conform to Topcased coding style");      
        FormData data7 = new FormData();
        data7.left = new FormAttachment(0,0);
        data7.right = new FormAttachment(100,-5);
        data7.top = new FormAttachment(genJavaDocButton,5); 
        useTopcasedChkstButton.setLayoutData(data7); 
        
        //Ouput label + text +button
        outputLabel = new Label(composite, SWT.NONE);
        outputLabel.setText("Output Folder");
        FormData data15 = new FormData();
        data15.left = new FormAttachment(0,0);
        data15.width = 75;
        outputLabel.setLayoutData(data15);
        
        outputText = new Text(composite, SWT.BORDER);
        FormData data14 = new FormData();
        data14.left = new FormAttachment(outputLabel,15);
        data14.width = 266;
        outputText.setLayoutData(data14);     
        
        browseButton = new Button(composite, SWT.PUSH);
        browseButton.setText("Browse...");        
        browseButton.addMouseListener(new browseButtonListener());
        FormData data16 = new FormData();
        data16.left = new FormAttachment(85,0);
        data16.width = 70;
        browseButton.setLayoutData(data16);  
       
        //Author label + text
        authorLabel = new Label(composite, SWT.NONE);
        authorLabel.setText("Author Name");
        FormData data8 = new FormData();
        data8.left = new FormAttachment(0,0);
        data8.top = new FormAttachment(outputLabel,15);
        data8.width = 75;
        authorLabel.setLayoutData(data8);
        
        authorText = new Text(composite, SWT.BORDER);
        FormData data9 = new FormData();
        data9.left = new FormAttachment(outputLabel,15);
        data9.top = new FormAttachment(outputLabel,15);       
        data9.width = 266;
        authorText.setLayoutData(data9);          
        
        //Header label + text
        headerLabel = new Label(composite, SWT.NONE);
        headerLabel.setText("Header");
        FormData data12 = new FormData();
        data12.left = new FormAttachment(0,0);
        data12.width = 75;
        data12.top = new FormAttachment(authorLabel,15);
        headerLabel.setLayoutData(data12);
        
        headerText = new Text(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        FormData data13 = new FormData();
        data13.left = new FormAttachment(headerLabel,17);
        data13.width = 250;
        data13.height = 100;
        data13.top = new FormAttachment(authorText,15);
        headerText.setLayoutData(data13);    
        
        // Populate owner text field
        try 
        {   
            String s;

            s =((IResource) getElement()).getPersistentProperty(new QualifiedName("", "GEN_AUTHOR"));
            if (s != null)
            {
                if (s.compareTo("true") == 0)
                {
                    genAuthorButton.setSelection(true);
                }
                else
                {
                    genAuthorButton.setSelection(false);
                }
            }
            else
            {
                genAuthorButton.setSelection(true);
            }
            
            s =((IResource) getElement()).getPersistentProperty(new QualifiedName("", "GEN_HEADER"));
            if (s != null)
            {
                if (s.compareTo("true") == 0)
                {
                    genHeaderButton.setSelection(true);     
                }
                else
                {
                    genHeaderButton.setSelection(false);
                }
            }
            else 
            {
                genHeaderButton.setSelection(true);
            }
            
            s =((IResource) getElement()).getPersistentProperty(new QualifiedName("", "GEN_ACCESSORS"));
            if (s != null)
            {
                if (s.compareTo("true") == 0)
                {
                    genAccessorsButton.setSelection(true);
                }
                else
                {
                    genAccessorsButton.setSelection(false);
                }
            }
            else
            {
                genAccessorsButton.setSelection(true);
            }
            
            s =((IResource) getElement()).getPersistentProperty(new QualifiedName("", "GEN_JAVADOC"));
            if (s != null)
            {
                if (s.compareTo("true") == 0)
                {
                    genJavaDocButton.setSelection(true);
                }
                else
                {
                    genJavaDocButton.setSelection(false);
                }
            }
            else
            {
                genJavaDocButton.setSelection(true);
            }
                
            s =((IResource) getElement()).getPersistentProperty(new QualifiedName("", "USE_CHECKSTYLE"));
            if (s != null)
            {
                if (s.compareTo("true") == 0)
                {
                    useTopcasedChkstButton.setSelection(true);
                }
                else
                {
                    useTopcasedChkstButton.setSelection(false);
                }       
            }
            else
            {
                useTopcasedChkstButton.setSelection(true);
            }        
            
            s =((IResource) getElement()).getPersistentProperty(new QualifiedName("", "AUTHOR_NAME"));
            if (s != null)
            {
                authorText.setText(s);
            }
            else
            {
                authorText.setText("your_name_here");
            }
            
            s =((IResource) getElement()).getPersistentProperty(new QualifiedName("", "HEADER_TEXT"));
            if (s != null)
            {
                headerText.setText(s);
            }
            else
            {
                headerText.setText(getDefaultHeader());
            }     
            
            s =((IResource) getElement()).getPersistentProperty(new QualifiedName("", "OUTPUT_PATH"));
            if ((s != null)&&(s.compareTo("") != 0))
            {
                outputText.setText(s);
            }
            else
            {
                IPath modelPath =   ((IResource) getElement()).getLocation();
                modelPath = modelPath.removeLastSegments(1);
                IPath workspacePath =   ((IResource) getElement()).getWorkspace().getRoot().getLocation();
                if (modelPath.removeFirstSegments(workspacePath.segmentCount()).getDevice() != null)
                {
                	s = modelPath.removeFirstSegments(workspacePath.segmentCount()).toString().substring(modelPath.removeFirstSegments(workspacePath.segmentCount()).getDevice().toString().length());
                }
                else
                {
                	s = modelPath.removeFirstSegments(workspacePath.segmentCount()).toString();
                }
                outputText.setText('/' + s);
            }      
            
        } 
        catch (CoreException e) 
        {
            genAuthorButton.setSelection(true);
            genHeaderButton.setSelection(true);
            genAccessorsButton.setSelection(true);
            genJavaDocButton.setSelection(true);
            useTopcasedChkstButton.setSelection(true);
            authorText.setText("your_name_here");
            headerText.setText(getDefaultHeader());  
            IPath modelPath =   ((IResource) getElement()).getLocation();
            modelPath = modelPath.removeLastSegments(1);
            IPath workspacePath =   ((IResource) getElement()).getWorkspace().getRoot().getLocation();
            String s = null;
            if (modelPath.removeFirstSegments(workspacePath.segmentCount()).getDevice() != null)
            {
            	s = modelPath.removeFirstSegments(workspacePath.segmentCount()).toString().substring(modelPath.removeFirstSegments(workspacePath.segmentCount()).getDevice().toString().length());
            }
            else
            {
            	s = modelPath.removeFirstSegments(workspacePath.segmentCount()).toString();
            }
            outputText.setText('/' + s);
        }
    }

    /**
     * @see PreferencePage#createContents(Composite)
     */
    protected Control createContents(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        FormLayout layout = new  FormLayout();
        composite.setLayout(layout);
        FormData data = new FormData();
        composite.setLayoutData(data);

        addFirstSection(composite);
        
        return composite;
    }

    /**
     * Create default composite
     * @param parent is the parent composite
     * @return the create composite
     */
    private Composite createDefaultComposite(Composite parent) {
        Composite composite = new Composite(parent, SWT.NULL);
        FormLayout layout = new FormLayout();
        composite.setLayout(layout);
        FormData data = new FormData();
        composite.setLayoutData(data);

        return composite;
    }
    
    /**
     * Initalize every widget to a default value
     */
    protected void performDefaults() 
    {
        genAuthorButton.setSelection(true);
        genHeaderButton.setSelection(true);     
        genAccessorsButton.setSelection(true);   
        genJavaDocButton.setSelection(true);
        useTopcasedChkstButton.setSelection(true);
        authorText.setText("your_name_here");
        headerText.setText(getDefaultHeader());     
        IPath modelPath =   ((IResource) getElement()).getLocation();
        modelPath = modelPath.removeLastSegments(1);
        IPath workspacePath =   ((IResource) getElement()).getWorkspace().getRoot().getLocation();
        String s = "";
        if (modelPath.removeFirstSegments(workspacePath.segmentCount()).getDevice() != null)
        {
        	s = modelPath.removeFirstSegments(workspacePath.segmentCount()).toString().substring(modelPath.removeFirstSegments(workspacePath.segmentCount()).getDevice().toString().length());
        }
        else
        {
        	s = modelPath.removeFirstSegments(workspacePath.segmentCount()).toString();
        }
        outputText.setText('/' + s);
    }

    /**
     * Convert a boolean to a string
     * @param b is the boolean
     * @return a string
     */
    public String boolToString(boolean b)
    {
        if (b)
        {
            return "true";
        }
        else
        {
            return "false";
        }
    }
    
    /**
     * Event executed when validating the property page. All properties are saved by Eclipse.
     */
    public boolean performOk() {
        // store the value in the owner text field
        try 
        {
            ((IResource) getElement()).setPersistentProperty(new QualifiedName("", "GEN_AUTHOR"), boolToString(genAuthorButton.getSelection()));
            ((IResource) getElement()).setPersistentProperty(new QualifiedName("", "GEN_HEADER"), boolToString(genHeaderButton.getSelection()));
            ((IResource) getElement()).setPersistentProperty(new QualifiedName("", "GEN_ACCESSORS"), boolToString(genAccessorsButton.getSelection()));
            ((IResource) getElement()).setPersistentProperty(new QualifiedName("", "GEN_JAVADOC"), boolToString(genJavaDocButton.getSelection()));        
            ((IResource) getElement()).setPersistentProperty(new QualifiedName("", "USE_CHECKSTYLE"), boolToString(useTopcasedChkstButton.getSelection()));
            ((IResource) getElement()).setPersistentProperty(new QualifiedName("", "AUTHOR_NAME"),authorText.getText());
            ((IResource) getElement()).setPersistentProperty(new QualifiedName("", "HEADER_TEXT"), headerText.getText());
            ((IResource) getElement()).setPersistentProperty(new QualifiedName("", "OUTPUT_PATH"), outputText.getText());
        } 
        catch (CoreException e) 
        {
            return false;
        }

        return true;
    }

    /**
     * Mouse listener for the "browse" button. It only open an Eclipse dialog for selecting the output folder.
     * @author aRamphul
     */
    private class browseButtonListener implements MouseListener 
    {

        public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent arg0)
        {
        }

        public void mouseDown(org.eclipse.swt.events.MouseEvent arg0)
        {    
            ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), null, true, "Select output :");
            dialog.open();
            Object[] objects = dialog.getResult();
            if(objects != null && objects.length == 1)
            {
                if(objects[0] instanceof IPath)
                {
                    IPath p = (IPath) objects[0];
                    if(p.segmentCount() > 0)
                    {
                        String s = p.toPortableString();
                        outputText.setText(s);
                    }
                }
            }
        }

        public void mouseUp(org.eclipse.swt.events.MouseEvent arg0)
        {
        }

    }
    
    /**
     * Get the default header text
     * @return the default header text
     */
    private String getDefaultHeader()
    {
        String header;
        header = "/*******************************************************************************\n";
        header = header + " * Copyright (c) 2011 Your Company Here. All rights reserved. This program\n";
        header = header + " * and the accompanying materials are made available under the terms of the\n";        
        header = header + " * Eclipse Public License v1.0 which accompanies this distribution, and is\n";
        header = header + " * available at http://www.eclipse.org/legal/epl-v10.html\n";      
        header = header + " *\n";
        header = header + " * Contributors: Based on UML 2 Java code generator by TOPCASED \n";
        header = header + "*******************************************************************************/\n"; 
        return header;
    }

}